import 'package:flutter/cupertino.dart';
import 'package:imember/Util/Global.dart';
import 'package:imember/Util/MyColor.dart';
import 'package:imember/Util/PageUtil.dart';
import 'package:imember/Widget/MyButton.dart';
import 'package:imember/Widget/MyImage.dart';
import 'package:imember/Widget/MyInput.dart';
import 'package:imember/Widget/MyPage.dart';

import 'CashExpend_AmountConfirm.dart';

class CashExpend extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return CashExpendState();
  }
}

class CashExpendState extends State {
  List<TextEditingController> amountControllers = [];
  List<TextEditingController> reasonControllers = [];

  @override
  Widget build(BuildContext context) {
    return MyPage(
        Container(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Expanded(
                  child: ListView.builder(
                      itemCount: amountControllers.length + 1,
                      itemBuilder: (BuildContext context, int index) {
                        if (index == amountControllers.length) {
                          return addBtn(() {
                            setState(() {
                              amountControllers.add(TextEditingController());
                              reasonControllers.add(TextEditingController());
                            });
                          });
                        } else {
                          return ItemExpend(delClick: () {
                            setState(() {
                              amountControllers.removeAt(index);
                              reasonControllers.removeAt(index);
                            });
                          });
                        }
                      })),
              OrangeBtn("下一步 >", width: 111, height: 35, margin: EdgeInsets.fromLTRB(0, 24, 60, 44),onTap: (){
                PageUtil().open(context, CashExpend_AmountConfirm());
              })
            ],
          ),
        ),
        title: "現金櫃臨時支出");
  }
}

class addBtn extends StatelessWidget {
  Function() onTap;

  addBtn(this.onTap);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      margin: EdgeInsets.only(right: 32),
      alignment: Alignment.centerRight,
      child: GestureDetector(
          onTap: onTap,
          behavior: HitTestBehavior.translucent,
          child: Row(mainAxisSize: MainAxisSize.min, children: [
            LocalImage("ic_add_o.png", width: 20, padding: EdgeInsets.all(11)),
            Text("新增臨時支出", style: TextStyle(fontSize: 19, color: MyColor.Orange))
          ])),
    );
  }
}

class ItemExpend extends StatelessWidget {
  Function()? delClick;

  ItemExpend({this.delClick});

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.fromLTRB(30, 12, 30, 12),
      padding: EdgeInsets.fromLTRB(37, 0, 37, 0),
      height: 123,
      decoration: Global.getWhiteBg_shadow(16, MyColor.C9),
      child: Row(
        children: [
          Expanded(
              child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              Row(children: [
                Text("金額", style: TextStyle(fontSize: 19, color: MyColor.C70)),
                Container(width: 15),
                Expanded(
                    child: Container(
                        height: 32, decoration: Global.getBoxBg(16, MyColor.F2), padding: EdgeInsets.only(left: 16), child: NormalInput()))
              ]),
              Row(children: [
                Text("事由", style: TextStyle(fontSize: 19, color: MyColor.C70)),
                Container(width: 15),
                Expanded(
                    child: Container(
                        height: 32, decoration: Global.getBoxBg(16, MyColor.F2), padding: EdgeInsets.only(left: 16), child: NormalInput()))
              ]),
            ],
          )),
          Container(width: 35),
          LocalImage("ic_x_g.png", width: 36, height: 36, onTap: delClick)
        ],
      ),
    );
  }
}
